package com.lindroid.spannablestringdemo;

import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.BackgroundColorSpan;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.ImageSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.StrikethroughSpan;
import android.text.style.StyleSpan;
import android.text.style.SubscriptSpan;
import android.text.style.SuperscriptSpan;
import android.text.style.URLSpan;
import android.text.style.UnderlineSpan;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Spannable.SPAN_EXCLUSIVE_EXCLUSIVE：前后都不包括，即在指定范围的前面和后面插入新字符都不会应用新样式
 * Spannable.SPAN_EXCLUSIVE_INCLUSIVE	：前面不包括，后面包括。即仅在范围字符的后面插入新字符时会应用新样式
 * Spannable.SPAN_INCLUSIVE_EXCLUSIVE	：前面包括，后面不包括。
 * Spannable.SPAN_INCLUSIVE_INCLUSIVE	：前后都包括。
 */
public class MainActivity extends AppCompatActivity {
    private TextView tvString;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvString = (TextView) findViewById(R.id.tvString);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add("SpannableString");
        menu.add("SpannableStringBuilder");
        menu.add("设置前景色");
        menu.add("设置背景色");
        menu.add("设置下划线");
        menu.add("设置删除线");
        menu.add("设置文字风格");
        menu.add("设置文字大小");
        menu.add("设置上标");
        menu.add("设置下标");
        menu.add("设置图片");
        menu.add("设置可点击文本");
        menu.add("设置超链接文本");
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getTitle().toString()) {
            case "SpannableString":
                createString();
                break;
            case "SpannableStringBuilder":
                appendString();
                break;
            case "设置前景色":
                setForegroundColor();
                break;
            case "设置背景色":
                setBackgroundColor();
                break;
            case "设置下划线":
                setUnderline();
                break;
            case "设置删除线":
                setStrikeThrough();
                break;
            case "设置文字风格":
                setStyle();
                break;
            case "设置文字大小":
                setSize();
                break;
            case "设置上标":
                setSuperscript();
                break;
            case "设置下标":
                setSubscript();
                break;
            case "设置图片":
                setImage();
                break;
            case "设置可点击文本":
                setClickable();
                break;
            case "设置超链接文本":
                setUrl();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * 设置超链接文本
     */
    private void setUrl() {
        SpannableString string = setSpannableString("请点击下载链接");
        URLSpan span = new URLSpan("https://www.baidu.com/") {
            @Override
            public void updateDrawState(TextPaint ds) {
                ds.setColor(Color.BLUE); //可点击文本字体颜色
                ds.setUnderlineText(false); //是否给可点击文本设置下划线
            }
        };
        string.setSpan(span, 3, string.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        tvString.setMovementMethod(LinkMovementMethod.getInstance()); //设置此方法才会响应点击事件
        tvString.setText(string);
    }

    /**
     * 设置可点击文本
     */
    private void setClickable() {
        SpannableString string = setSpannableString("请点击阅读原文");
        ClickableSpan span = new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                Toast.makeText(MainActivity.this, "阅读原文", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(Color.BLUE); //可点击文本字体颜色
                ds.setUnderlineText(false); //是否给可点击文本设置下划线
            }

        };
        string.setSpan(span, 3, string.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        tvString.setMovementMethod(LinkMovementMethod.getInstance()); //设置此方法才会响应点击事件
        tvString.setHighlightColor(Color.TRANSPARENT); //设置点击后的背景色
        tvString.setText(string);
    }

    /**
     * 设置图片
     */
    private void setImage() {
        SpannableString string = setSpannableString("来一张新鲜的图片");
//        ImageSpan span = new ImageSpan(this,R.mipmap.ic_launcher_round,ImageSpan.ALIGN_BASELINE);
//        string.setSpan(span,6,string.length(),Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        Drawable d = getResources().getDrawable(R.mipmap.ic_launcher_round);
        d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
        ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
        string.setSpan(span, 6, string.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        tvString.setText(string);
    }

    /**
     * 设置上标
     */
    private void setSubscript() {
        SpannableString string = setSpannableString("这是下标");
        SubscriptSpan span = new SubscriptSpan();
        string.setSpan(span, 2, 4, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        tvString.setText(string);
    }

    /**
     * 设置上标
     */
    private void setSuperscript() {
        SpannableString string = setSpannableString("E=mc2");
        SuperscriptSpan span = new SuperscriptSpan();
        string.setSpan(span, 4, string.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        tvString.setText(string);
    }

    /**
     * 设置文字大小
     */
    private void setSize() {
        SpannableString string = setSpannableString("大珠小珠落玉盘");
        //设置绝对大小
        AbsoluteSizeSpan as = new AbsoluteSizeSpan(sp2px(32));
        //设置相对大小
        RelativeSizeSpan rs = new RelativeSizeSpan(0.8f);
        string.setSpan(as, 0, 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        string.setSpan(rs, 2, 3, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        tvString.setText(string);
    }

    /**
     * 设置文字风格
     */
    private void setStyle() {
        SpannableString string = setSpannableString("斜风细雨不须归");
        StyleSpan italic = new StyleSpan(Typeface.ITALIC); //设置斜体
        StyleSpan bold = new StyleSpan(Typeface.BOLD); //设置粗体
        string.setSpan(italic, 0, 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        string.setSpan(bold, 2, 3, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        tvString.setText(string);
    }

    /**
     * 设置删除线
     */
    private void setStrikeThrough() {
        SpannableString string = setSpannableString("土豆、西红柿、马铃薯");
        StrikethroughSpan span = new StrikethroughSpan();
        string.setSpan(span, 3, 6, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        tvString.setText(string);

    }

    /**
     * 设置下划线
     */
    private void setUnderline() {
        SpannableString string = setSpannableString("重要的事情说三次");
        UnderlineSpan span = new UnderlineSpan();
        string.setSpan(span, 6, string.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        tvString.setText(string);
        tvString.setTextColor(Color.RED);
    }


    /**
     * 设置前景色
     */
    private void setForegroundColor() {
        SpannableString string = setSpannableString("蓝色是忧郁的颜色");
        ForegroundColorSpan span = new ForegroundColorSpan(Color.BLUE);
        string.setSpan(span, 0, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        tvString.setText(string);

    }

    /**
     * 设置背景色
     */
    private void setBackgroundColor() {
        SpannableString string = setSpannableString("恭喜发财");
        BackgroundColorSpan span = new BackgroundColorSpan(Color.RED);
        string.setSpan(span, 0, string.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        tvString.setTextColor(Color.WHITE);
        tvString.setText(string);
    }

    private SpannableString setSpannableString(CharSequence source) {
        SpannableString spannableString = new SpannableString(source);
        return spannableString;
    }


    /**
     * 使用SpannableString创建字符串
     */
    private void createString() {
        SpannableString spannableString = new SpannableString("Hello World!");
        tvString.setText(spannableString);
    }

    /**
     * 使用SpannableStringBuilder拼接字符串
     */
    private void appendString() {
        SpannableStringBuilder sb = new SpannableStringBuilder();
        sb.append("Keep ");
        sb.append("going!");
        tvString.setText(sb);
    }

    /**
     * 将sp值转换为px值
     *
     * @param spValue
     * @return
     */
    public int sp2px(float spValue) {
        final float fontScale = getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5);
    }
}
